<template>
	<view class="address-list-page">
		<view class="address-list-content">
			<hsb-business-address-list
				:list="addressList"
				@edit="manageAddress"
				@choose="chooseAddress"
			/>
			<view
				v-if="noAddress"
				class="no-address-data"
			>
				<image class="icon-address" src="@/static/images/icon_no_address.png"></image>
				<view class="no-address-text">地址库空空如也， 快去新增地址吧～</view>
			</view>
		</view>
		<view class="button-content">
			<view
				class="button"
				@click="manageAddress"
			>
				<text>新增地址</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	computed: {
		noAddress () {
			return this.addressList.length === 0 && !this.loading && this.hasLoaded
		}
	},
	data () {
		return {
			loading: false,
			hasLoaded: false,
			addressList: []
		}
	},
	methods: {
		async getAddressList () {
			this.loading = true
			const [res] = await this.$apis.getAllowAddressList({
				pageSize: 50
			})
			this.loading = false
			if (res._errCode === '0') {
				this.addressList = res._data.list || []
			}
			this.hasLoaded = true
		},
		manageAddress (addrInfo = {}) {
			uni.navigateTo({
				url: `/pages/address/recycle-save/index?addrId=${addrInfo.addrId || ''}`
			})
		},
		chooseAddress (addrInfo) {
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.emit('updateRefundAddressInfo', {
				data: addrInfo
			})
			uni.navigateBack()
		}
	},
	onShow () {
		this.getAddressList()
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
